<template>
  <div>
    <!-- 碎片列表 -->
    <my-breadcrumb :items="['抽奖设置', '碎片列表']"/>
    <search :formItems='searchItems' @search='getList' ref='search' />
    <el-table @sort-change="sortChange" :data='tableData' style='width: 100%'>
      <el-table-column type="index" label='序号' ></el-table-column>
      <el-table-column prop='prizename' label='碎片名称' ></el-table-column>
      <el-table-column prop='membername' label='用户名称' ></el-table-column>
      <el-table-column prop='fragmentcount' sortable label='碎片个数'></el-table-column>
      <el-table-column prop='status' label='是否失效'>
        <template slot-scope="scope">
          <span v-if="scope.row.status === '1'">否</span>
          <span v-if="scope.row.status === '0'">是</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import utils from '@/utils/utils'
export default {
  name: 'fragmentationList',
  data () {
    return {
      tableData: [],
      searchItems: [
        {
          label: '碎片名称',
          type: 'input',
          key: 'prizename'
        },
        {
          label: '用户名称',
          type: 'input',
          key: 'membername'
        },
        {
          label: '是否失效',
          type: 'select',
          key: 'status',
          options: [{ id: '1', name: '否' }, { id: '0', name: '是' }]
        }
      ],
      page: {
        page: 1,
        size: 20,
        total: 1
      },
      sortVal: {}
    }
  },
  methods: {
    getList (page = 1, order = '') {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {
        ...searchData,
        page: this.page.page,
        size: this.page.size
      }
      if (typeof page === 'number') {
        params.page = page
      }
      if (this.sortVal.prop) {
        params.orderby = this.sortVal.prop + ' ' + this.sortVal.order
      }
      this.axios.get('/Luckdraw/fragmentindex', { params }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          utils.tableItemToNumber(res.data.data.list, ['fragmentcount'])
          this.page.total = Number(res.data.data.count)
          this.page.page = page
          this.tableData = res.data.data.list
        }
      })
    },
    handleSizeChange (val) {
      this.page.size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getList(val)
    },
    sortChange (data) {
      this.sortVal.prop = data.prop
      if (data.order === 'ascending') {
        this.sortVal.order = 'asc'
      } else if (data.order === 'descending') {
        this.sortVal.order = 'desc'
      } else {
        this.sortVal.order = ''
      }
      this.getList()
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
<style scoped>
</style>
